package com.akk.module.avs.service;

import com.akk.module.avs.entity.AvsIncreRecord;
import com.akk.module.avs.entity.AvsIncreRecordExample;
import com.akk.module.avs.mapper.AvsIncreRecordMapper;
import com.akk.module.avs.mapper.dao.AvsIncreRecordDao;
import com.akk.module.avs.model.response.AvsIncreRecordRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AvsIncreReocrdService {

    @Autowired
    private AvsIncreRecordExample recordExample;

    @Autowired
    private AvsIncreRecordMapper recordMapper;

    @Autowired
    private AvsIncreRecordDao increRecordDao;

    public List<AvsIncreRecordRes> queryByParamsAndTd(Map params){
        return increRecordDao.selectByParams(params);
    }

    public List<AvsIncreRecord> queryByParams(Map params){
        recordExample.clear();
        AvsIncreRecordExample.Criteria criteria = recordExample.createCriteria();
        if (params.containsKey("typeId"))
            criteria.andTypeIdEqualTo(Long.valueOf(params.get("typeId").toString()));
        if (params.containsKey("type"))
            criteria.andTypeEqualTo(params.get("type").toString());
        if (params.containsKey("startDate") && params.containsKey("endDate")){
            Date s = stringToDate(params.get("startDate").toString());
            Date e = stringToDate(params.get("endDate").toString());
            if (s!=null && e!=null)
                criteria.andCreateTimeBetween(s,e);
        }
        return recordMapper.selectByExample(recordExample);
    }

    public AvsIncreRecord queryById(Long recordId){
        return recordMapper.selectByPrimaryKey(recordId);
    }

    public void save(AvsIncreRecord increRecord){
        recordMapper.insert(increRecord);
    }

    public Date stringToDate(String dateStr){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        try {
            return format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}
